package pl.vanta.mjuzik.gui.panels;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

import pl.vanta.mjuzik.ApplicationConstants;
import pl.vanta.mjuzik.gui.listeners.PlaylistButtonsListener;
import pl.vanta.mjuzik.services.ActionService;
import pl.vanta.utils.gui.panels.VListenerPanel;

public class PlaylistButtonsPanel extends VListenerPanel<PlaylistButtonsListener> implements ActionListener
{
	private static final long	serialVersionUID	= 1L;

	private static final String	ID_BTN_ADD			= "ID_BTN_ADD";
	private static final String	ID_BTN_REMOVE		= "ID_BTN_REMOVE";
	private static final String	ID_BTN_CLEAR		= "ID_BTN_CLEAR";
	private static final String	ID_BTN_LOAD			= "ID_BTN_LOAD";
	private static final String	ID_BTN_SAVE			= "ID_BTN_SAVE";
	private static final String	ID_BTN_CLEAR_RATING	= "ID_BTN_CLEAR_RATING";
	private static final String	ID_BTN_OPTIONS		= "ID_BTN_OPTIONS";

	private JButton				btnAdd;
	private JButton				btnRemove;
	private JButton				btnClear;

	private JButton				btnLoadPlaylist;
	private JButton				btnSavePlayList;

	private JButton				btnClearRating;

	private JButton				btnOptions;

	private ActionService		actionService;

	public PlaylistButtonsPanel(ActionService actionService)
	{
		this.actionService = actionService;

		initComponents();
		setupComponents();
		layoutComponents();
	}

	public void initComponents()
	{
		this.btnAdd = new JButton("Add");
		this.btnRemove = new JButton("Remove");
		this.btnClear = new JButton("Clear");

		this.btnLoadPlaylist = new JButton("Load playlist");
		this.btnSavePlayList = new JButton("Save playlist");

		this.btnClearRating = new JButton("Clear rating");

		this.btnOptions = new JButton(actionService.getAction(ApplicationConstants.ACTION_OPTIONS));
	}

	public void setupComponents()
	{
		this.btnAdd.setActionCommand(ID_BTN_ADD);
		this.btnRemove.setActionCommand(ID_BTN_REMOVE);
		this.btnClear.setActionCommand(ID_BTN_CLEAR);
		this.btnLoadPlaylist.setActionCommand(ID_BTN_LOAD);
		this.btnSavePlayList.setActionCommand(ID_BTN_SAVE);
		this.btnClearRating.setActionCommand(ID_BTN_CLEAR_RATING);
		this.btnOptions.setActionCommand(ID_BTN_OPTIONS);

		this.btnAdd.addActionListener(this);
		this.btnRemove.addActionListener(this);
		this.btnClear.addActionListener(this);
		this.btnLoadPlaylist.addActionListener(this);
		this.btnSavePlayList.addActionListener(this);
		this.btnClearRating.addActionListener(this);
		this.btnOptions.addActionListener(this);
	}

	public void layoutComponents()
	{
		this.setLayout(new GridLayout(7, 1));

		this.add(this.btnAdd);
		this.add(this.btnRemove);
		this.add(this.btnClear);

		this.add(this.btnLoadPlaylist);
		this.add(this.btnSavePlayList);

		this.add(this.btnClearRating);

		this.add(this.btnOptions);
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		for (PlaylistButtonsListener listener : this.listeners)
		{
			if (ID_BTN_ADD.equals(e.getActionCommand()))
			{
				listener.addFiles();
			}
			else if (ID_BTN_REMOVE.equals(e.getActionCommand()))
			{
				listener.removeFiles();
			}
			else if (ID_BTN_CLEAR.equals(e.getActionCommand()))
			{
				listener.clearPlaylist();
			}
			else if (ID_BTN_LOAD.equals(e.getActionCommand()))
			{
				listener.loadPlaylist();
			}
			else if (ID_BTN_SAVE.equals(e.getActionCommand()))
			{
				listener.savePlaylist();
			}
			else if (ID_BTN_CLEAR_RATING.equals(e.getActionCommand()))
			{
				listener.clearRating();
			}
		}
	}
}